<?php
/**
 * File which contains the cPhotoAlbum-class
 *
 * @package Classes
 */

/**
 * Class for a photoalbum to make, delete and get some albuminfo
 *
 * @author Xander Hoogland <home@xanland.nl>
 * @package Classes
 */
class cPhotoAlbum
{
    /**
     * Path to the dir of the albums, set in the constructor
     *
     * @access private
     * @var string
     */
    private $sPath;

    /**
     * Name of the album, set in a method
     *
     * @access private
     * @var string
     */
    private $sAlbumName;

    /**
     * Constructor which sets the path to the albumdir
     *
     * @access public
     */
    public function __construct ()
    {
	$this -> sPath = '../../public/uploads/foto-album/albums/';
    }

    /**
     * For creating an album
     *
     * @access public
     * @param string $sAlbumName
     * @return boolean
     */
    public function create ($sAlbumName)
    {
		$this -> sAlbumName = $sAlbumName;

		if ($this -> itemExists ($this -> sPath . $this -> sAlbumName))
		{
			return false;
		}
		else
		{
			if (mkdir ($this -> sPath . $this -> sAlbumName) && mkdir ($this -> sPath . $this -> sAlbumName . '/thumbs'))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
    }

    /**
     * For deleting
     *
     * @access public
     * @param string $sAlbumName
     * @return boolean
     */
    public function delete ($sAlbumName)
    {
	$this -> sAlbumName = $sAlbumName;

	if (!$this -> itemExists ($this -> sPath . $this -> sAlbumName))
	{
	    return false;
	}
	else
	{
	    if (rmdir ($this -> sPath . $this -> sAlbumName) || rmdir ($this -> sPath . $this -> sAlbumName . '/thumbs'))
	    {
		return true;
	    }
	    else
	    {
		return false;
	    }
	}
    }

    /**
     * Checking if an album or picture exists
     *
     * @access public
     * @return boolean
     */
    public function itemExists ()
    {
	if (file_exists ($this -> sPath . $this -> sAlbumName))
	{
	    return true;
	}
	else
	{
	    return false;
	}
    }

    /**
     * Getting a list of all the albums available
     *
     * @access public
     * @return array
     */
    public function getList ()
    {
	$a_sAlbums = array ();

	foreach (scandir ($this -> sPath) as $sAlbum)
	{
	    if ($sAlbum == '.' || $sAlbum == '..')
	    {
		//
	    }
	    else
	    {
		$a_sAlbums[] = $sAlbum;
	    }
	}

	return $a_sAlbums;
    }

    /**
     * Getting all the images in an album
     *
     * @access public
     * @param string $sAlbumName
     * @param boolean $bFrontpage
     * @return mixed
     */
    public function imageList ($sAlbumName, $bFrontpage = false)
    {
	$this -> sAlbumName = $sAlbumName;

	if ($bFrontpage == true)
	{
	    $sPath = str_replace ('../../p', 'p', $this -> sPath);
	}
	else
	{
	    $sPath = $this -> sPath;
	}

	foreach (scandir ($sPath . $this -> sAlbumName) as $sImage)
	{
	    if ($sImage == '.' || $sImage == '..' || $sImage == 'thumbs')
	    {
		//
	    }
	    else
	    {
		$a_sImages[] = $sImage;
	    }
	}

	if (isset ($a_sImages))
	{
	    return $a_sImages;
	}
	else
	{
	    return false;
	}
    }

    /**
     * Get a random image for the album cover
     *
     * @access public
     * @param string $sAlbumName
     * @return string
     */
    public function getRandomImage ($sAlbumName)
    {
	$this -> sAlbumName = $sAlbumName;

	$a_sImageNames = array ();

	foreach (scandir (str_replace ('../../p', 'p', $this -> sPath) . $this -> sAlbumName) as $sImageName)
	{
	    if ($sImageName == '.' || $sImageName == '..' || $sImageName == 'thumbs')
	    {
		//
	    }
	    else
	    {
		$a_sImageNames[] = $sImageName;
	    }
	}

	$iRandKey = array_rand ($a_sImageNames);

	if (isset ($iRandKey))
	{
	    return $a_sImageNames [$iRandKey];
	}
	else
	{
	    return '../../../../../images/placeholder.jpg';
	}
    }
}